package com.sgb.kjwl.view.ui.activity.leaseAsk;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityMyAskLeaseBinding;
import com.sgb.kjwl.di.machine.DaggerMachineComponent;
import com.sgb.kjwl.di.machine.MachineModule;
import com.sgb.kjwl.model.entity.LeasePublishEntity;
import com.sgb.kjwl.model.entity.lease.AskLeaseItemEntity;
import com.sgb.kjwl.view.ui.activity.leaseAsk.publish.AskLeaseFactoryActivity2;
import com.sgb.kjwl.view.ui.activity.leaseAsk.publish.AskLeaseFarmActivity2;
import com.sgb.kjwl.view.ui.activity.leaseAsk.publish.AskLeaseHouseActivity2;
import com.sgb.kjwl.view.ui.activity.leaseAsk.publish.AskLeaseLandActivity2;
import com.sgb.kjwl.view.ui.activity.leaseAsk.publish.PublishLeaseInfoActivity2;
import com.sgb.kjwl.view.ui.fragment.machine.MyAskLeaseFragment;
import com.sgb.kjwl.view.widget.LeasePublishDialog;
import com.sgb.kjwl.viewmodel.machine.MyAskLeaseModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

//我的求租
public class MyAskLeaseActivity extends AppActivity {

    @Inject
    public MyAskLeaseModel viewModel;

    private ActivityMyAskLeaseBinding binding;
    private FragPagerAdapter pagerAdapter;
    private ArrayList<Fragment> mFragments;
    private String[] titles;
    private List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>> cidList = new ArrayList<>();

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMachineComponent.builder().appComponent(appComponent)
                .machineModule(new MachineModule(this))
                .build()
                .inject(this);
    }

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, MyAskLeaseActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_my_ask_lease);
        binding.topLayout.titleView.setText("需求发布");
        binding.topLayout.ivBack.setVisibility(View.VISIBLE);
        binding.topLayout.ivBack.setOnClickListener(view -> finish());
        binding.topLayout.rightView.setText("发布");
        binding.topLayout.rightView.setOnClickListener(v -> showBottomDialog());
        binding.tablayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                onResetTextSize();
                binding.tablayout.getTitleView(position).setTextSize(18);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

    }

    private void onResetTextSize() {
        int count = binding.tablayout.getTabCount();
        for (int i = 0; i < count; i++) {
            binding.tablayout.getTitleView(i).setTextSize(15);
        }
    }

    @Override
    protected void init() {
        viewModel.getTypeModel();

        mFragments = new ArrayList<>();
    }

    public void showTypeView(List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>> datas) {
        if (datas == null || datas.size() == 0) {
            return;
        }

        cidList = datas;

        mFragments = new ArrayList<>();
        titles = new String[datas.size()];
        for (int i = 0; i < datas.size(); i++) {
            ArrayList<AskLeaseItemEntity<List<AskLeaseItemEntity>>> arrayList = new ArrayList<>();
            arrayList.addAll(datas.get(i).getChildren());
            mFragments.add(MyAskLeaseFragment.newInstance(datas.get(i).getType(), i, arrayList));
            titles[i] = datas.get(i).getName();
        }
        pagerAdapter = new FragPagerAdapter(getSupportFragmentManager(), mFragments);
        binding.viewpager.setAdapter(pagerAdapter);
        binding.viewpager.setOffscreenPageLimit(titles.length - 1);
        binding.tablayout.setViewPager(binding.viewpager, titles);
        binding.tablayout.setCurrentTab(0);
        binding.tablayout.getTitleView(0).setTextSize(18);
    }

    public void changeType(int position) {
        binding.viewpager.setCurrentItem(position);
    }

    private void showBottomDialog() {
        LeasePublishDialog leasePublishDialog = new LeasePublishDialog(getActivity(), null);
        leasePublishDialog.setData(getList());
        leasePublishDialog.setDialogClickCallBack(new LeasePublishDialog.DialogClickCallBack() {
            @Override
            public void onItemClick(String s) {
                switch (s) {
                    case "械"://机械
                        PublishLeaseInfoActivity2.start(MyAskLeaseActivity.this, 0);
                        break;
                    case "设": //设备
                        PublishLeaseInfoActivity2.start(MyAskLeaseActivity.this, 1);
                        break;
                    case "车"://车辆
                        PublishLeaseInfoActivity2.start(MyAskLeaseActivity.this, 2);
                        break;
                    case "房"://房产
                        AskLeaseHouseActivity2.start(MyAskLeaseActivity.this);
                        break;
                    case "地"://土地
                        AskLeaseLandActivity2.start(MyAskLeaseActivity.this);
                        break;
                    case "厂"://工厂
                        AskLeaseFactoryActivity2.start(MyAskLeaseActivity.this);
                        break;
                    case "农"://农机
                        AskLeaseFarmActivity2.start(MyAskLeaseActivity.this);
                        break;
                }
            }
        }).show();
    }

    private List<LeasePublishEntity> getList() {
        List<LeasePublishEntity> list = new ArrayList<>();
        list.add(new LeasePublishEntity("机械求租", "械"));
        list.add(new LeasePublishEntity("设备求租", "设"));
        list.add(new LeasePublishEntity("车辆求租", "车"));
        list.add(new LeasePublishEntity("房产求租", "房"));
        list.add(new LeasePublishEntity("土地求租", "地"));
        list.add(new LeasePublishEntity("工厂求租", "厂"));
        list.add(new LeasePublishEntity("农机求租", "农"));
        return list;
    }
}
